package com.example.stock.ui.query;

import android.annotation.SuppressLint;
import android.app.Application;
import android.util.Log;

import com.example.stock.base.BaseViewModel;
import com.example.stock.base.Result;
import com.example.stock.model.GoodsData;
import com.example.stock.model.GoodsOrderBean;
import com.example.stock.util.DBUtil;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class QueryViewModel extends BaseViewModel {
    public MutableLiveData<Result<List<GoodsData>>> mBeans = new MutableLiveData<>();
    public MutableLiveData<Integer> mCount = new MutableLiveData<>();

    public QueryViewModel(@NonNull Application application) {
        super(application);
    }

    @SuppressLint("CheckResult")
    public void loadAllByType(GoodsOrderBean bean, int indexType){
        getShowDialog().setValue(true);
        Observable.just(0).map(new Function<Integer, Integer>() {
            @NonNull
            @Override
            public Integer apply(@NonNull Integer integer) throws Exception {
                List<GoodsData> data =  DBUtil.getInstance().queryGoodsDataByDjh(bean, indexType, Integer.MAX_VALUE);
                if(data == null || data.size() == 0){
                    return 0;
                }
                return data.size();
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Integer>() {
                    @Override
                    public void accept(@NonNull Integer beans) throws Exception {
                        mCount.setValue(beans);
                    }
                });
    }

    @SuppressLint("CheckResult")
    public void listByDB(GoodsOrderBean bean, int indexType, int offset) {
        getShowDialog().setValue(true);
        Observable.just(offset).map(new Function<Integer, List<GoodsData>>() {
            @NonNull
            @Override
            public List<GoodsData> apply(@NonNull Integer integer) throws Exception {
                return DBUtil.getInstance().queryGoodsDataByDjh(bean, indexType, integer);
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<List<GoodsData>>() {
                    @Override
                    public void accept(@NonNull List<GoodsData> beans) throws Exception {
                        getShowDialog().setValue(false);
                        Result<List<GoodsData>> result = new Result<>();
                        if (beans.size() > 0) {
                            result.setCode("0001");
                        } else {
                            result.setCode("0002");
                            result.setMsg("数据为空");
                        }
                        result.setData(beans);
                        mBeans.setValue(result);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@NonNull Throwable throwable) throws Exception {
                        getShowDialog().setValue(false);
                        Result<List<GoodsData>> result = new Result<>();
                        result.setCode("0003");
                        result.setMsg("e : " + throwable.getMessage());
                        mBeans.setValue(result);
                    }
                });
    }
}
